package com.lgr.store.controller;

import com.lgr.store.service.DepService;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.DepVo;
import com.lgr.store.vo.EmpVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/dep")
public class DepController {
    @Resource
    private DepService depService;
    @Resource
    HttpSession session;

    @RequestMapping(value="/list")
    public String list(ModelMap map){
        PageObject pager=new PageObject();
        List<DepVo> depList = depService.list();
        pager.setDatas(depList);
        map.addAttribute("pager",pager);
        map.addAttribute("empList",depService.listEmp());
        return "depList";
    }
    //新增或修改
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, ModelMap map, DepVo depVo, @RequestParam(value = "depid")Integer depid){

        if(depService.findById(depid) != null){
            //修改
//            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(2,depVo));
            depService.update(depVo);
        }else{
            //新增
//            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(1,depVo));
            depService.add(depVo);
        }
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        //重新设置查询条件
        depVo = new DepVo();
        return list(map);
    }
    //删除
    @RequestMapping(value = "/del")
    public String del(HttpServletRequest request,Integer depid,ModelMap map){
        DepVo depVo = new DepVo();
//        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(3,depVo));
        depService.delete(depid);
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        return list(map);
    }
    @RequestMapping(value = "/json")
    @ResponseBody
    public DepVo json(@RequestParam("depid") Integer depid) {
        return depService.findById(depid);
    }
    //判断是否有子节点
    @RequestMapping(value = "/isDepParentId")
    @ResponseBody
    public List<DepVo> isDepParentId(int parentId) {
        List<DepVo> list=depService.findByParentId(parentId);
        return list;
    }
    //判断是部门下是否有员工
    @RequestMapping(value = "/isEmpDepId")
    @ResponseBody
    public List<EmpVo> isEmpDepId(int depid) {
        List<EmpVo> list=depService.selectEmp(depid);
        return list;
    }
}
